<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" inline label-width="100px">
            <el-form-item label="分账状态：">
              <el-radio-group v-model="tableFrom.fz_status" type="button" @change="getList(1)">
                <el-radio-button label="">全部 </el-radio-button>
                <el-radio-button label="-1">不支持分账</el-radio-button>
                <el-radio-button label="-2">分账失败</el-radio-button>
                <el-radio-button label="0">未分账</el-radio-button>
                <el-radio-button label="1">分账成功</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="时间选择：" class="width100">

              <el-date-picker
                v-model="timeVal"
                value-format="yyyy/MM/dd"
                format="yyyy/MM/dd"
                size="small"
                type="daterange"
                placement="bottom-end"
                placeholder="自定义时间"
                style="width: 250px;"
                @change="onchangeTime"
              />
            </el-form-item>
            <el-form-item label="关键字：" class="width100">
              <el-input
                v-model="tableFrom.keyword"
                placeholder="请输入付款手机号"
                class="selWidth"
                size="small"
                @keyup.enter.native="getList(1)"
              >
                <el-button slot="append" icon="el-icon-search" size="small" class="el-button-solt" @click="getList(1)" />
              </el-input>
            </el-form-item>
<!--            <el-form-item label="分账状态：">-->
<!--              <el-select-->
<!--                v-model="tableFrom.status"-->
<!--                placeholder="请选择"-->
<!--                class="filter-item selWidth mr20"-->
<!--                clearable-->
<!--                @change="getList"-->
<!--              >-->
<!--                <el-option-->
<!--                  v-for="item in invoiceStatusList"-->
<!--                  :key="item.value"-->
<!--                  :label="item.label"-->
<!--                  :value="item.value"-->
<!--                />-->
<!--              </el-select>-->
<!--            </el-form-item>-->
            <el-form-item label="付款类型：">
              <el-radio-group v-model="tableFrom.pay_type" type="button" @change="getList(1)">
                <el-radio-button label="">全部 </el-radio-button>
                <el-radio-button label="0">余额</el-radio-button>
                <el-radio-button label="1">微信</el-radio-button>
                <el-radio-button label="2">支付宝</el-radio-button>
                <el-radio-button label="3">现金券</el-radio-button>
                <el-radio-button label="4">支付宝-小程序</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="付款状态：">
              <el-radio-group v-model="tableFrom.status" type="button" @change="getList(1)">
                <el-radio-button label="">全部 </el-radio-button>
                <el-radio-button label="0">未付款</el-radio-button>
                <el-radio-button label="1">已付款</el-radio-button>

              </el-radio-group>
            </el-form-item>
<!--            <el-form-item label="订单号：" class="width100">-->
<!--              <el-input-->
<!--                v-model="tableFrom.username"-->
<!--                placeholder="请输入订单号"-->
<!--                class="selWidth"-->
<!--                size="small"-->
<!--                @keyup.enter.native="getList(1)"-->
<!--              >-->
<!--                <el-button slot="append" icon="el-icon-search" size="small" class="el-button-solt" @click="getList(1)" />-->
<!--              </el-input>-->
<!--            </el-form-item>-->

          </el-form>
        </div>
      </div>
      <el-table
        ref="multipleSelection"
        v-loading="listLoading"
        tooltip-effect="dark"
        :row-key="(row) => { return row.order_receipt_id }"
        :data="tableData.data"
        style="width: 100%"
        class="table"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          :reserve-selection="true"
          width="55"
        />
        <el-table-column prop="out_trade_no" label="订单号" min-width="170" />
        <el-table-column prop="account" label="付款手机号" min-width="90" />
        <el-table-column prop="amount" label="付款金额" min-width="90" />
        <el-table-column label="分账金额" min-width="90" prop="share_money"></el-table-column>

        <el-table-column  label="分账状态" min-width="180" >
          <template slot-scope="scope">
            <span v-if="scope.row.fz_status!=-2">{{ getFZStatus(scope.row.fz_status) }}</span>
            <span v-else style="color: red"> {{scope.row.fz_fail}}</span>
          </template>
        </el-table-column>
        <el-table-column  label="分账状态" min-width="180" >
          <template slot-scope="scope">
            <span >{{ scope.row.status == 0 ?  '未付款':'已付款' }}</span>
            <!--            <span v-else> {{scope.row.fz_fail}}</span>-->
          </template>
        </el-table-column>
        <el-table-column  label="付款类型" min-width="120" >
          <template slot-scope="scope">
            <span >{{ getPayType(scope.row.pay_type) }}</span>
<!--            <span v-else>&#45;&#45;</span>-->
          </template>
        </el-table-column>
        <el-table-column label="分账时间" min-width="100" prop="create_time">

        </el-table-column>
<!--        <el-table-column label="操作" min-width="180" fixed="right" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            <el-button v-if="scope.row.status === 0 && scope.row.storeOrder && scope.row.storeOrder.paid === 1" type="text" size="small" @click="getInvoiceInfo(scope.row.order_receipt_id,'invoice')">开票</el-button>-->
<!--            <el-button type="text" size="small" @click="onOrderDetail(scope.row.order_id)">订单详情</el-button>-->
<!--            <el-button v-if="scope.row.status" type="text" size="small" @click="getInvoiceInfo(scope.row.order_receipt_id,'edit')">编辑</el-button>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--开票-->

  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  billListApi,
  invoiceUpdateApi,
  invoiceDetailApi,
  invoiceInfoApi,
  invoiceApi
} from '@/api/order'

export default {
  name: 'OrderInvoice',
  data() {
    return {
      logisticsName: 'refund',
      id: 0,
      type: '',
      tableData: {
        data: [],
        total: 0
      },
      invoiceStatusList: [
        { label: '全部',value: null },
        { label: '不支持分账', value: '-1' },
        { label: '已分账失败开票', value: '-2' },
        { label: '未分账', value: '0' },
        { label: '分账成功', value: '1'},

      ],
      listLoading: true,
      tableFrom: {
        pay_type: null,//支付类型 0，1，,2，,3，,4 ye'，'wx'，'zf''zf'rq',
        fz_status: null,//:-2,-1,0,1'
        date: null,
        page: 1,
        limit: 20,
        keyword: null,
        status: null//|支付状态 0 ,1
      },
      orderChartType: {},
      timeVal: [],
      multipleSelection: [],
      fromList: {
        title: '选择时间',
        custom: true,
        fromTxt: [
          { text: '全部', val: '' },
          { text: '今天', val: 'today' },
          { text: '昨天', val: 'yesterday' },
          { text: '最近7天', val: 'lately7' },
          { text: '最近30天', val: 'lately30' },
          { text: '本月', val: 'month' },
          { text: '本年', val: 'year' }
        ]
      },
      selectionList: [],
      tableFromLog: {
        page: 1,
        limit: 20
      },
      tableDataLog: {
        data: [],
        total: 0
      },
      loading: false,
      dialogVisible: false,
      orderDatalist: null,
      invoiceInfo: {

      },
      invoiceData: {
        number: '',
        mark: ''
      }

    }
  },
  mounted() {

    this.getList(1)
  },

  methods: {
    // 订单详情
    onOrderDetail(order_id) {
      this.$router.push({
        name: 'offLineWater',
        query: {
          id: order_id
        }
      })
    },
    getFZStatus(status){
      switch (status) {
        case 0 :
          return '未分账'
          break
        case 1 :
          return '分账成功'
          break
        case -1 :
          return '不支持分账'
          break
        case -2 :
          return '分账失败'
          break

      }
    },
    getPayType(type){
      switch (type) {
        case 0 :
          return '余额支付'
          break
        case 1 :
          return '微信支付'
          break
        case 2 :
          return '支付宝支付'
          break
        case 3 :
          return '现金券支付'
          break
        case 4:
          return '支付宝小程序支付'
          break

      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    pageChangeLog(page) {
      this.tableFromLog.page = page
      this.getList('')
    },
    handleSizeChangeLog(val) {
      this.tableFromLog.limit = val
      this.getList('')
    },
    // 开票信息
    getInvoiceInfo(id, type) {
      this.id = id
      this.type = type
      const ids = id ? id.toString() : this.getInvoic()
      const params = { ids: ids }
      type === 'invoice'
        ? invoiceInfoApi(params)
          .then(res => {
            this.listLoading = false
            this.dialogVisible = true
            this.invoiceInfo = res.data
            this.invoiceData = {
              number: '',
              mark: ''
            }
          })
          .catch(res => {
            this.$message.error(res.message)
            this.listLoading = false
          })
        : invoiceDetailApi(id) // 编辑详情
          .then(res => {
            this.listLoading = false
            this.dialogVisible = true
            this.invoiceInfo = res.data
            this.invoiceData = {
              number: res.data.receipt_no,
              mark: res.data.mer_mark
            }
          })
          .catch(res => {
            this.$message.error(res.message)
            this.listLoading = false
          })
    },
    // 开票
    handleInvoic() {
      const ids = this.id ? this.id.toString() : this.getInvoic()
      const params = {
        ids: ids,
        receipt_sn: this.invoiceInfo.receipt_sn,
        receipt_price: this.invoiceInfo.receipt_price,
        receipt_no: this.invoiceData.number,
        mer_mark: this.invoiceData.mark ? this.invoiceData.mark : ''
      }
      this.type === 'invoice'
        ? invoiceApi(params)
          .then(res => {
            this.$message.success(res.message)
            this.dialogVisible = false
            this.getList()
            this.id = ''
          })
          .catch(res => {
            this.$message.error(res.message)
            this.listLoading = false
          })
        : invoiceUpdateApi(this.id, { receipt_no: this.invoiceData.number, mer_mark: this.invoiceData.mark ? this.invoiceData.mark : '' })
          .then(res => {
            this.$message.success(res.message)
            this.dialogVisible = false
            this.getList()
            this.id = ''
          })
          .catch(res => {
            this.$message.error(res.message)
            this.listLoading = false
          })
    },
    // 获取开票订单id
    getInvoic() {
      const ids = []
      this.multipleSelection.forEach(function(item, index) {
        ids.push(item.order_receipt_id)
      })
      return ids.toString()
    },
    // 编辑
    onOrderMark(id) {
      // this.$modalForm(invoiceorderMarkApi(id)).then(() => this.getList(''))
    },
    // 选择时间
    selectChange(tab) {
      this.tableFrom.date = tab
      this.timeVal = []
      this.getList(1)
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e
      this.tableFrom.date = e ? this.timeVal.join('-') : ''
      this.getList(1)
    },
    // 列表
    getList(num) {
      this.listLoading = true
      this.tableFrom.page = num || this.tableFrom.page
      if(this.tableFrom.fz_status){
        this.tableFrom.fz_status = parseInt(this.tableFrom.fz_status)
      }
      billListApi(this.tableFrom)
        .then(res => {
          this.orderChartType = res.data.stat
          this.tableData.data = res.data.list
          this.tableData.total = res.data.count
          this.listLoading = false
        })
        .catch(res => {
          this.$message.error(res.message)
          this.listLoading = false
        })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList()
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList()
    },

    handleClose() {
      this.dialogLogistics = false
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/ .el-dialog__title{
  font-weight: bold;
}
.selWidth {
  width: 300px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 12px;
}
.el-icon-arrow-down {
  font-size: 12px;
}

.tabBox_tit {
  width: 60%;
  font-size: 12px !important;
  margin: 0 2px 0 10px;
  letter-spacing: 1px;
  padding: 5px 0;
  box-sizing: border-box;
}
.box-container {
  overflow: hidden;
  padding: 0 10px;
}
.box-container .title{
  margin: 15px 0 10px;
  color: #333;
  font-weight: bold;
  border-bottom: 1px solid #dfe6ec;
}

.box-container .list {
  float: left;
  line-height: 40px;
}
.box-container .list .info{
  display: block;
  .el-textarea{
    margin-top: 10px;
  }
}

.box-container .sp {
  width: 50%;
}
.box-container .sp3 {
  width: 33.3333%;
}
.box-container .sp100 {
  width: 100%;
}
.box-container .list .name {
  align-items: center;
  width: 100px;
  color: #606266;
}
.acea-row{
  margin-bottom: 25px;
}
</style>
